﻿using System;
using System.Net;
using System.ServiceModel;

namespace FlightSearchServer
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("Usage: {0} <clients port> <sellers port>", AppDomain.CurrentDomain.FriendlyName);
                return;
            }

            try
            {
                Console.WriteLine("Starting registration service... ");

                var sellerRegistration = new ServiceHost(typeof (FlightSearchRegService),
                    new Uri(string.Format("http://localhost:{0}/Services/FlightsSearchReg", args[1])));

                sellerRegistration.Open();

                Console.WriteLine("OK");
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed ({0})", e.InnerException is WebException ? e.InnerException.Message : e.Message);
                return;
            }

            try
            {
                Console.WriteLine("Starting flight search service... ");

                var flightSearch = new ServiceHost(typeof (FlightSearchService),
                    new Uri(string.Format("http://localhost:{0}/Services/FlightsSearch", args[0])));
                flightSearch.Open();

                Console.WriteLine("OK");
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed ({0})", e.InnerException is WebException ? e.InnerException.Message : e.Message);
                return;
            }

            Console.WriteLine("Flight search service is running on port {0}", args[0]);

            Console.WriteLine("Press ENTER to stop service");
            Console.ReadLine();
        }
    }
}
